/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Portions Copyright [2007] [Cheng-Lung Sung]
 */
/*
 * WORD_FREQ function
 * $Id: word_freq.c 2 2008-10-25 08:17:21Z clsung $
 */
#include "char.h"
#include "word_freq.h"
#include <string.h>

WORD_FREQ *e_cursor;
extern FILE* debugfile;

int wt_flag; // Word type flag, 0001 -> ascii, 0010 -> utf8
int
lookupWordIndex (WORD_FREQ * word_array, const char* word, const int len) {
    WORD_FREQ *cursor = e_cursor;
    int cursor_length;

    if (e_cursor == 0)	return 0;
    cursor_length = cursor->wend - cursor->word;
    if (len > cursor_length) return (e_cursor-word_array+1);
    while (cursor >= word_array) { // at least we won't search over :-)
        cursor_length = cursor->wend - cursor->word;
        if (len < cursor_length) {
            cursor--;
            continue;
        }
        if (len > cursor_length || strncmp(cursor->word,word,len)) { // not equal
            return (e_cursor-word_array+1); // move forward
        }
        if (cursor_length == len && !strncmp(cursor->word,word,len)) {
            return (cursor-word_array); // do we need to devide sizeof(WORD_FREQ)?
        }
        cursor--;
    }
    fprintf(debugfile, "We have go thourgh the whole word_array!!\n");
    fflush(NULL);
    return (e_cursor-word_array+1);
}

unsigned int
lookupWordIndexForFreq (WORD_FREQ * word_array, WORD_FREQ * c_cursor) {
    WORD_FREQ *cursor = c_cursor;
    int c_cursor_length = c_cursor->wend - c_cursor->word;
    unsigned int toAddFreq = 0;
    if (cursor == e_cursor) return 0;
    while (cursor <= e_cursor ) { // at least we won't search over :-)
        int cursor_length = cursor->wend - cursor->word;
        if (!cursor_length || strncmp(c_cursor->word,cursor->word,c_cursor_length)) {
            break;
        }
        if ((wt_flag & ASCII) && ismyalpha(cursor->word[c_cursor_length]))  {
            break;
        }
        toAddFreq += (cursor->freq-1); // -1, since every freq start from 2
        cursor++;
    }
    toAddFreq -= (c_cursor->freq - 1); // don't add myself
    return toAddFreq;
}

